package util;

import java.io.*;
import java.net.*;
import java.util.regex.*;

import javax.sql.rowset.spi.*;
import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.*;

import ex.PlaceNotFoundException;
public class WeatherUtil {
	private static String getResponseWithRequest(String cityName) throws IOException{
		String result="";
		
		URL serviceURL=new URL("http://www.webservicex.net/globalweather.asmx");
		String params="<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
					+"<soap:Body>"
					+"<GetWeather xmlns=\"http://www.webserviceX.NET\">"
					+"<CityName>"+cityName+"</CityName>"
					+"<CountryName></CountryName>"
					+"</GetWeather>"
					+"</soap:Body>"
					+"</soap:Envelope>";
		
		HttpURLConnection connjava = (java.net.HttpURLConnection)serviceURL.openConnection();
	    connjava.setRequestMethod("POST");
	    connjava.setRequestProperty("Content-Length", "" + Integer.toString(params.getBytes().length));
	    connjava.setRequestProperty("Content-Language", "en-US"); 
	    connjava.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
	    connjava.setRequestProperty("SOAPAction", "http://www.webserviceX.NET/GetWeather");
	    connjava.setDoInput(true); 
	    connjava.setDoOutput(true); 
	    connjava.setUseCaches(false); 
	    connjava.setAllowUserInteraction(true);
	    DataOutputStream printout = new DataOutputStream (connjava.getOutputStream());
	    printout.writeBytes(params);
	    printout.flush();
	    printout.close();
	    BufferedReader in = new BufferedReader(new InputStreamReader(connjava.getInputStream()));
	    String line;            
	    while ((line = in.readLine()) != null) {
	    	result+=line;
	    }
	    in.close();
	
		
		return result;
	}
	
	public static double getDegree(String cityName) throws Exception
	{		
		String result="";
		String temperature="";
		String celcius="";
		double degree = -274;
		
		try {
			String response=getResponseWithRequest(cityName);
			
			Pattern pattern = Pattern.compile("<GetWeatherResult>(.*?)</GetWeatherResult>");
			Matcher matcher = pattern.matcher(response);
			if (matcher.find())
			{
				result=matcher.group(1);
				if(result.equals("Data Not Found"))
					throw new Exception(cityName + " not found!");
				else{
					pattern = Pattern.compile("&lt;Temperature&gt;(.*?)&lt;/Temperature&gt;");
					matcher = pattern.matcher(response);
					if (matcher.find())
					{
						temperature=matcher.group(1);
						pattern = Pattern.compile("\\((.*?)\\sC\\)");
						matcher = pattern.matcher(temperature);
						
						if (matcher.find()){
							celcius=matcher.group(1);	
							degree=Double.parseDouble(celcius);
						}
					}
				}
			}
		} catch (Exception e) {
			throw new Exception("Error getting weather form web service: "+e.getMessage());
		}
		return degree;			
	}
}
